package com.tmxk.api.days01.zjq;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

/**
 * @BelongsProject: java-api
 * @BelongsPackage: com.tmxk.api.days01.zjq
 * @Author: zjq
 * @CreateTime: 2022-11-28  16:07
 * @Description: TODO
 * @Version: 1.0
 */
public class SetDemo {
    public static void main(String[] args) {
        Dog dog1 = new Dog("ss",2);
        Dog dog2 = new Dog("花花",3);
        Dog dog3 = new Dog("小黑",1);
        Dog dog4 = new Dog("小白",5);
        Dog dog5 = new Dog("汪汪",6);

        Set<Dog> dogSet = new HashSet<>();
        dogSet.add(dog1);
        dogSet.add(dog2);
        dogSet.add(dog3);
        dogSet.add(dog4);
        dogSet.add(dog5);

        System.out.println(dog2.getName());

        //获取set集合元素
        //方法一
        for (Dog dog : dogSet){
            System.out.println(dog.getName()+"\t"+dog.getAge()+"\t\t");
        }


        //方法二 lambda表达式
        dogSet.forEach(dog -> {
            System.out.println(dog.getName()+"\t"+dog.getAge()+"\t");
        });

        //方法三  使用迭代器,Iterator可理解成专门为 集合框架遍历元素而产生的
        Iterator<Dog> it = dogSet.iterator();
        //it.hasNext();获取下一个迭代约束是否存在
        while(it.hasNext()){
            Dog dog = it.next();
            System.out.println(dog.getName()+"\t"+dog.getAge());
        }

        TreeSet dogTreeSet = new TreeSet();
        dogTreeSet.add(233);
        dogTreeSet.add(123);
        dogTreeSet.add(41242);
        dogTreeSet.add(2333);
        dogTreeSet.add(572);
        System.out.println("遍历TreeSet集合");
        for (Object obj : dogTreeSet){
            System.out.println(obj);
        }
    }
}

